
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>变量 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="解构.html" />
    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >变量</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x4EE5;&#x524D;&#x7684;&#x4E00;&#x4E9B;&#x95EE;&#x9898;:</p>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    for (var i = 0; i &lt; 10; i++) {
        //&#x8FD9;&#x91CC;&#x7684;i&#x662F;&#x5168;&#x5C40;&#x7684;...
    }
    alert(i); // 10
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var a = 12;
    function fn() {
        alert(a);
        var a = 5;
    }
    fn(); //undefined, 
    // &#x56E0;&#x4E3A;&#x4E0A;&#x9762;&#x76F8;&#x5F53;&#x4E8E;: 
    // var a;
    // alert(a);
    // a = 5;
&lt;/script&gt;
</code></pre>
<p>ES6&#x58F0;&#x660E;&#x53D8;&#x91CF;: </p>
<ul>
<li>let &#x76F8;&#x5F53;&#x4E8E; var, &#x4F46;&#x662F;let&#x5177;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</li>
<li>const &#x5E38;&#x91CF;,&#x5B9A;&#x4E49;&#x597D;&#x540E;&#x4E0D;&#x80FD;&#x6539;&#x53D8;</li>
</ul>
<h3 id="let">let</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let a = 12;
    function fn() {
        alert(a); // Uncaught ReferenceError: Cannot access &apos;a&apos; before initialization
        let a = 5;
    }
    fn();
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    for (let i = 0; i &lt; 9; i++) { //for&#x5FAA;&#x73AF;&#x91CC;&#x9762;&#x662F;&#x7236;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;
        let i = &quot;hello&quot;;
        console.log(i); //hello
    }
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    /**
    var arr = [];
    for (var i = 0; i &lt; 10; i++) { //&#x5BF9;&#x4E8E;&#x8FD9;&#x6837;var&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;i&#x662F;&#x5168;&#x5C40;&#x7684;
        arr[i] = function() {
            console.log(i);
        }
    }

    arr[1](); // 10
    arr[5](); // 10
    arr[9](); // 10
    */

    var arr = [];
    for (let i = 0; i &lt; 10; i++) { // &#x5BF9;&#x4E8E;&#x8FD9;&#x6837;let&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;i&#x662F;&#x5C40;&#x90E8;&#x7684;
        arr[i] = function() {
            console.log(i);
        }
    }

    arr[1](); // 1
    arr[5](); // 5
    arr[9](); // 9
&lt;/script&gt;

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;var&lt;/title&gt;
    &lt;!-- &lt;meter name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;&lt;/meter&gt; --&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        window.onload = function() {
            var aInput = document.querySelectorAll(&apos;input&apos;);
            for (let i = 0; i &lt; aInput.length; i++) {
                aInput[i].onclick = function() {
                    alert(i);
                }
            }
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;input type=&quot;button&quot; value=&quot;aaa&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;bbb&quot; /&gt;
    &lt;input type=&quot;button&quot; value=&quot;ccc&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h3 id="const">const</h3>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    const a = 5;
    a = 6;
    console.log(a); //Uncaught TypeError: Assignment to constant variable.
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    const a;
    a = 6;
    console.log(a); //Uncaught SyntaxError: Missing initializer in const declaration
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    const arr = [&apos;apple&apos;, &apos;banana&apos;];
    arr.push(&apos;orange&apos;);
    console.log(arr); //[&apos;apple&apos;, &apos;banana&apos;, &apos;orange&apos;]
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    const arr = Object.freeze([&apos;apple&apos;, &apos;banana&apos;]);
    arr.push(&apos;orange&apos;);
    console.log(arr); //error 
&lt;/script&gt;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: Introduction">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="解构.html" class="navigation navigation-next " aria-label="Next page: 解构">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"变量","level":"1.2","depth":1,"next":{"title":"解构","level":"1.3","depth":1,"path":"解构.md","ref":"解构.md","articles":[]},"previous":{"title":"Introduction","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"变量.md","mtime":"2021-11-11T16:19:26.360Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

